class RESSequenceCode(object):
    """
    Model:
        满足序列号的所有算法逻辑

    Param:
        - env: odoo env

    Function:
        - explore_sequence: 根据消息获取错误代码，如果没有获取到，则创建一个并返回代码追加序列号
    """

    def __init__(self, env):
        self.env = env

    def explore_sequence(self, model, prefix, key, padding=3):
        """ 每探索一次，相应记录 index +1 """
        model_rcd = self.env['ir.model'].search([('model', '=', model.replace('_', '.'))])
        rcd = self.env['res.sequence.code'].search([('model_id', '=', model_rcd.id), ('prefix', '=', prefix)], limit=1)
        if not rcd:
            rcd = self.env['res.sequence.code'].create({'model_id': model_rcd.id,
                                                        'prefix': prefix,
                                                        'padding': padding,
                                                        'key': key})
        print_code = rcd.prefix + '_' + rcd.key + '/' + str(rcd.index).zfill(rcd.padding)  # 先输出
        rcd.index = rcd.index + 1  # 再 +1
        return print_code

    def clean_serial(self, serial: str) -> str:
        # 将除第三位之外的所有'/'转换为'_'
        return serial[:2] + serial[2:].replace("/", "_")

    def restore_serial(self, cleaned_serial: str) -> str:
        # 将所有的'_'转回'/'，但保留第三位的下划线
        return cleaned_serial[:3] + cleaned_serial[3:].replace("_", "/")
